/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases.domain.services;

import clases.domain.Bodega;
import clases.domain.Modelo;
import clases.domain.Pieza;
import clases.domain.Version;
import conexion.util.MysqlConfigUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionException;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 *
 * @author Diego
 */
public class MantenedorModeloService {
     //Inserta Bodega
    public boolean insertModelo(Modelo modelo) throws Exception {
        boolean inserta = false;
        int insert = 0;
        SqlSessionFactory sqlSessionFactory = MysqlConfigUtil.getSqlSessionFactory();
        SqlSession session = sqlSessionFactory.openSession();
        try {
            System.out.println("inserto");
            insert = session.insert("Modelo.insertModelo", modelo);
            session.commit();
            if (insert == 1) {
                inserta = true;
            } else {
                System.out.println("Error al insertar");
            }
        } catch (SqlSessionException e) {
            System.out.println("inserto");
        }
        return inserta;
    }

    //Actualiza Bodega
    public boolean updateModelo(Modelo modelo) throws Exception {
        boolean update = false;
        int updat = 0;
        SqlSessionFactory sqlSessionFactory = MysqlConfigUtil.getSqlSessionFactory();
        SqlSession session = sqlSessionFactory.openSession();
        try {
            System.out.println("Actualiza");
            updat = session.update("Modelo.updateModelo", modelo);
            session.commit();
            if (updat == 1) {
                update = true;
            } else {
                System.out.println("Error al actualizar");
            }
        } catch (SqlSessionException e) {
            System.out.println("Actualizo");
        }
        return update;
    }

    //Elimina Fabrica
    public boolean deleteModelo(Modelo modelo) throws Exception {
        boolean delete = false;
        int delet = 0;
        SqlSessionFactory sqlSessionFactory = MysqlConfigUtil.getSqlSessionFactory();
        SqlSession session = sqlSessionFactory.openSession();
        try {
            System.out.println("Elimino");
            delet = session.delete("Modelo.deleteModelo", modelo);
            session.commit();
            if (delet == 1) {
                delete = true;
            } else {
                System.out.println("Error al Eliminar");
            }
        } catch (SqlSessionException e) {
            System.out.println("Elimino");
        }
        return delete;
    }

    //Lista Modelos
    public List<Modelo> getListAllModelos() throws Exception {
        SqlSessionFactory sqlSessionFactory = MysqlConfigUtil.getSqlSessionFactory();
        SqlSession session = sqlSessionFactory.openSession();
        List<Modelo> listModelos = null;
        try {
            listModelos = session.selectList("Modelo.selectAllModelos");
            for (Iterator<Modelo> it = listModelos.iterator(); it.hasNext();) {
                Modelo modelo = it.next();
                 List<Version> listVersiones = null;
                 listVersiones=modelo.getVersiones();
                 for (Iterator<Version> it1 = listVersiones.iterator(); it1.hasNext();) {
                    Version version = it1.next();
                    version.setModelo(modelo);
                }
            }
            System.out.println("Trae Lista");
        } catch (SqlSessionException e) {
            System.out.println("Error");
        }
          finally {
            session.close();
        }
        return listModelos;
    }
}
